﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.PhanQuyen;
using DTTB.DTO;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.PhanQuyen;
using DTTB.SERVICE;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.PhanQuyen
{
    public class RolesServiceDelegate
    {
        PhanQuyenDataContext PhanQuyenDB = new PhanQuyenDataContext();

        RolesService RolesService = new RolesService();
        PathsService PathsService = new PathsService();
        RolesInPathsService RolesInPathsService = new RolesInPathsService();
        UsersInRolesService UsersInRolesService = new UsersInRolesService();

        //cũ
        //public IList<RolesDto> GetAllRoles()
        //{
        //    return RolesService.GetAllRoles();
        //}

        public IList<RolesDto> GetAllRolesIsEnable()
        {
            return RolesService.GetAllRolesIsEnable();
        }

        public RolesDto GetRolesByName(string name)
        {
            return RolesService.GetRolesByName(name);
        }

        public RolesDto GetRolesByID(string id)
        {
            return RolesService.GetRolesByID(id);
        }

        public RolesDto GetRolesDeletedByName(string name)
        {
            return RolesService.GetRolesByName(name);
        }

        public bool AddNewRoles(RolesDto rolesDto)
        {
            return RolesService.AddNewRoles(rolesDto);
        }

        public bool Update(RolesDto rolesDto)
        {
            return RolesService.Update(rolesDto);
        }

        public bool DeleteRoles(string ID)
        {
            return RolesService.DeleteRoles(ID);
        }

        public IList<PathsDto> GetAllPathsByModulesAndPermissionType(string modules, string permissionType)
        {
            return this.PathsService.GetAllPathsByModulesAndPermissionType(modules, permissionType);
        }

        public RolesDto BuildNewRoles(RolesForm form)
        {
            RolesDto rolesDto = new RolesDto();

            rolesDto.RoleName = form.RoleName;
            rolesDto.LoweredRoleName = form.RoleName.ToLower();
            rolesDto.Description = form.Description;
            rolesDto.IsDelete = false;
            rolesDto.IsEnable = form.IsEnable;
            rolesDto.CreateDate = DateTime.Now;
            rolesDto.UpdateDate = DateTime.Now;

            rolesDto.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            rolesDto.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);

            return rolesDto;
        }

        
        public RolesDto BuildEditRoles(RolesForm form)
        {
            RolesDto rolesDto = new RolesDto();

            rolesDto = RolesService.GetRolesByID(form.RolesID);// Lấy thông tin cũ của role trong DataBase

            //Cập nhật lại một số thuộc tính mới
            rolesDto.RoleName = form.RoleName;
            rolesDto.LoweredRoleName = form.RoleName.ToLower();
            rolesDto.Description = form.Description;
            rolesDto.IsDelete = false;
            rolesDto.IsEnable = form.IsEnable;
            rolesDto.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            rolesDto.UpdateDate = DateTime.Now;

            return rolesDto;
        }

        public bool ShiftDelete(string roleID)
        {
            bool result = false;
            result = RolesService.ShiftDelete(roleID); //Xóa role

            //Xóa mọi Paths đã gán cho role
            if(result == true )
                RolesInPathsService.DeleteRolesInPathsByRolesId(roleID);

            return result;
        }

        public IList<PathsDto> GetPaths()
        {
            return this.PathsService.GetAllPaths();
        }

        public IList<RolesInPathsDto> GetRolesInPathsByRolesId(string roleID)
        {
            return this.RolesInPathsService.GetRolesInPathsByRolesId(roleID);
        }

        public void UpdateManageRolesWithPermissions(IList<RolesInPathsDto> rolesInPathsDtos, string roleId)
        {
            this.RolesService.UpdateManageRolesWithPages(rolesInPathsDtos, roleId);
        }

        public IList<UsersInRolesDto> GetUsersInRolesByRoleId(string roleId)
        {
            return UsersInRolesService.GetUsersInRolesByRoleId(roleId);
        }


        #region Phần code chuuyển sang LinQ

        /// <summary>
        /// Lấy tất cả Role không bị đánh dấu delete
        /// </summary>
        /// <returns>Danh sách Role</returns>
        public IList<TB_Role> GetAllRoles()
        {
            return PhanQuyenDB.TB_Roles.Where(p => p.IsDelete == false).OrderBy(p => p.RoleName).ToList();
        }

        public SelectList BuildDepartmentSelectList()
        {
            IList<TB_Role> list;

            list = GetAllRoles();//test

            IList<object> selectList = new List<object> { new { value = 0, Text = "[-- Chọn quyền --]" } };

            foreach (var dto in list)
            {
                selectList.Add(new { value = dto.ID, Text = dto.RoleName });
            }

            return new SelectList(selectList, "Value", "Text");
        }

        public void BuildSelectListForRolesForm(RolesForm form)
        {
            form.TieuChiSearchDropDownList = BuildSelectListSearchRoles();
        }

        /// <summary>
        /// Build selectList - Tiêu chí tìm kiếm role
        /// </summary>
        /// <returns>SelectList</returns>
        public SelectList BuildSelectListSearchRoles()
        {
            IList<object> tieuChiSearchList = new List<object>();
            tieuChiSearchList.Add(new { value = 1, Text = "Tên quyền" });
            //tieuChiSearchList.Add(new{value = 1, Text = "..."});

            return new SelectList(tieuChiSearchList, "Value", "Text");
        }

        /// <summary>
        /// Tìm kiếm quyền theo một tiêu chí
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>Danh sách các quyền</returns>
        public IList<TB_Role> SearchRoles(int key, string value)
        {
            IList<TB_Role> roleList = new List<TB_Role>();
            var roles = from role in PhanQuyenDB.TB_Roles
                              where role.IsDelete == false
                              select role;
            if (key == 1) // Tìm quyền theo Tên
                roleList = roles.Where(p => p.RoleName.Contains(value)).OrderBy(p => p.RoleName).ToList();

            return roleList;
        }


        #endregion
    }
}
